/* $RCSFile: LocalizedDescriptionAttribute.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.ServerControls/LicenceControls/LocalizedDescriptionAttribute.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

// LocalizedDescriptionAttribute.cs
// Server Controls and Components
//

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace ADNCRM.ServerControls {

    [AttributeUsage(AttributeTargets.All)]
    internal sealed class LocalizedDescriptionAttribute : DescriptionAttribute 
    {

        private bool _localized;

        public LocalizedDescriptionAttribute(string resourceKey) : base(resourceKey) 
        {}

        public override string Description 
        {
            get 
            {
                // Localize the description the first time it is accessed
                // by using the current description as the name of the resource
                // and replacing it with the string value retrieved from
                // the resource manager
                if (_localized == false) 
                {
                    _localized = true;

                    try 
                    {
                        string resourceKey = base.Description;
                        string localizedDescription = AssemblyResourceManager.GetString(resourceKey);

                        Debug.Assert(localizedDescription != null, "Resource not found: '" + resourceKey + "'");

                        if (localizedDescription != null) 
                        {
                            base.DescriptionValue = localizedDescription;
                        }
                    }
                    catch {}
                }

                return base.Description;
            }
        }
    }
}
